package com.zhaoyizhe.demo.controller;

import com.zhaoyizhe.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 默认是发生RuntimeException和Error发生回滚
    @Transactional
    @RequestMapping("/u1")
    public void u1(Integer id,String name) {
        userService.u1(id,name);
        if (true) {
            int a = 10/0;
        }
    }

    //设置noRollbackFor 表示所有的异常都不进行回滚
    @Transactional(noRollbackFor = Exception.class)
    @RequestMapping("/u2")
    public void u2(Integer id,String name) {
        userService.u1(id,name);
        if (true) {
            int a = 10/0;
        }
    }


    // 设置所有状态下都发生回滚
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/u3")
    public void u3(Integer id,String name) {
        userService.u1(id,name);
        throw new RuntimeException();
    }

    //设置所有状态下都发生回滚 设置隔离级别为默认级别  默认级别同数据库级别一样  数据库默认级别为  可重复读
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT)
    @RequestMapping("/u4")
    public void u4(Integer id,String name) {
        userService.u1(id,name);
        throw new RuntimeException();
    }


}
